<?php
class question extends adminController {
    public $_indexView = "admin/question/index";
    public $_listView = "admin/question/list";
    public $_addView = "admin/question/add";
    public $_editView = "admin/question/edit";
    
	public function __construct() {
		parent::__construct ();
	}
	
    public function loadView($temp) {
        if (!isset($temp["data"]))
            $temp["data"] = '';
        $this->load->view("admin/index", $temp);
    }
    
    public function index($state=1) {
        $temp["template"] = $this->_listView;
        if($state==1) {
            $temp['op'] = 1;
            $data = $this->questionModel->get();
        } else {
            $data = $this->questionModel->get_by('answerid', 0);
            $temp['op'] = 0;
        }
        
        $result = array();
        $accounts = array(0=>'');
        foreach($data as $k=>$v) {
            if(!isset($accounts[$v['accountid']])) {
                $acc = $this->accountModel->read($v['accountid']);
                $accounts[$acc['id']] = $acc['username'];
            }
            if(!isset($accounts[$v['answerid']])) {
                $acc = $this->accountModel->read($v['answerid']);
                $accounts[$acc['id']] = $acc['username'];
            }
            $v['accountid'] = $accounts[$v['accountid']];
            $v['answerid'] = $accounts[$v['answerid']];
            $result[] = $v;
        }
        
        $temp["data"] = $result;
        $this->loadView($temp);
    }
    
    public function add() {
        $temp["template"] = $this->_addView;
        if(!$this->isPost()) {
            $this->loadView($temp);
            return;   
        }
        $data = $this->input->post();
        $slug = mb_strtolower(url_title(removesign($data['title'])));
        if($this->questionModel->check_exist('slug', $slug)) {
            $slug = $slug.'-'.random_string('alnum', 4);
        }
        $data['slug'] = $slug;
        $account = $this->session->userdata('account');
        $data['accountid'] = $account['id'];
        $data['answerid'] = $account['id'];
        $data['startdate'] = date('Y-m-d H:i:s');
        $data['enddate'] = date('Y-m-d H:i:s');
        $data['views'] = 1;
        $data['statuss'] = 1;
        
        if(!$data) {
            $data['answerid'] = 0;
        }
        
        $this->questionModel->insert($data);
        $this->index();
    }
    
    public function edit($id) {
        $temp["template"] = $this->_editView;
        $question = $this->questionModel->read($id);
        if(!$this->isPost()) {
            $temp["data"] = $question;
            $this->loadView($temp);
            return;   
        }
        $data = $this->input->post();
        if($data['answer'] != $question['answer']) {
            $account = $this->session->userdata('account');
            $data['answerid'] = $account['id'];
            $data['enddate'] = date('Y-m-d H:i:s');
        }
        if(!$data['answer']) $data['answerid'] = 0;
        $this->questionModel->update($data, array('id' => $id));
        redirect(base_url().$this->_indexView);
    }
    
    public function delete($id) {
        $this->questionModel->delete($id);
        redirect(base_url().$this->_indexView);
    }
}
?>